using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;

namespace CANBusTools.ViewModels
{
    public class RctaControlViewModel : ViewModelBase
    {
        private readonly object _busHelper;

        public string[] Sides { get; } = { "1.Left", "2.Right" };
        public string[] States { get; } = { "Invalid", "OFF", "ON" };

        private string _selectedSide = string.Empty;
        public string SelectedSide
        {
            get => _selectedSide;
            set
            {
                if (_selectedSide != value)
                {
                    _selectedSide = value;
                    OnPropertyChanged(nameof(SelectedSide));
                }
            }
        }

        private string _selectedState = string.Empty;
        public string SelectedState
        {
            get => _selectedState;
            set
            {
                if (_selectedState != value)
                {
                    _selectedState = value;
                    OnPropertyChanged(nameof(SelectedState));
                }
            }
        }

        public ICommand SetRctaCommand { get; }

        public RctaControlViewModel(object busHelper)
        {
            _busHelper = busHelper;
            SelectedSide = Sides[0];
            SelectedState = States[1];
            SetRctaCommand = new RelayCommand(SetRcta);
        }

        private void SetRcta()
        {
            try
            {
                int index = int.Parse(SelectedSide.Split('.')[0]);
                int value = Array.IndexOf(States, SelectedState);
                // TODO: 替换为实际调用 _busHelper.RCTAWarningSwitch(index, value);
                Console.WriteLine($"Set RCTA Warning: Side={index}, State={value}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设置 RCTA 警告失败: {ex.Message}");
            }
        }
    }
}
